package com.ruoyi.crm.sale.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.sale.domain.CrmBusiness;
import com.ruoyi.crm.sale.domain.CrmBusinessProduct;
import com.ruoyi.crm.sale.domain.CrmContract;
import com.ruoyi.crm.sale.mapper.CrmBusinessProductMapper;
import com.ruoyi.crm.sale.mapper.CrmContractMapper;
import com.ruoyi.crm.sale.service.ICrmBusinessProductService;
import com.ruoyi.crm.sale.service.ICrmBusinessService;

/**
 * 商机产品关系Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-14
 */
@Service
public class CrmBusinessProductServiceImpl implements ICrmBusinessProductService 
{
    @Autowired
    private CrmBusinessProductMapper crmBusinessProductMapper;
    @Autowired
    private ICrmBusinessService crmBusinessService;
    @Autowired
    private CrmContractMapper crmContractMapper;

    /**
     * 查询商机产品关系
     * 
     * @param id 商机产品关系ID
     * @return 商机产品关系
     */
    @Override
    public CrmBusinessProduct selectCrmBusinessProductById(Long id)
    {
        return crmBusinessProductMapper.selectCrmBusinessProductById(id);
    }

    /**
     * 查询商机产品关系列表
     * 
     * @param crmBusinessProduct 商机产品关系
     * @return 商机产品关系
     */
    @Override
    public List<CrmBusinessProduct> selectCrmBusinessProductList(CrmBusinessProduct crmBusinessProduct)
    {
        return crmBusinessProductMapper.selectCrmBusinessProductList(crmBusinessProduct);
    }

    /**
     * 新增商机产品关系
     * 
     * @param crmBusinessProduct 商机产品关系
     * @return 结果
     */
    @Override
    public int insertCrmBusinessProduct(CrmBusinessProduct crmBusinessProduct)
    {
        crmBusinessProduct.setCreateTime(DateUtils.getNowDate());
        return crmBusinessProductMapper.insertCrmBusinessProduct(crmBusinessProduct);
    }

    /**
     * 修改商机产品关系
     * 
     * @param crmBusinessProduct 商机产品关系
     * @return 结果
     */
    @Override
    public int updateCrmBusinessProduct(List<CrmBusinessProduct> crmBusProductList)
    {
    	if(crmBusProductList.size() > 0) {
    		//删除商机所有商品后重新添加
    		crmBusinessProductMapper.deleteCrmBusProductByBusId(crmBusProductList.get(0).getBusinessId(), Long.valueOf(1));
    		BigDecimal totalPrice = new BigDecimal(0);
    		for(CrmBusinessProduct busPro : crmBusProductList) {
    			busPro.setCreateTime(DateUtils.getNowDate());
    			busPro.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    			if(null == busPro.getProId()) {
    				busPro.setProId(busPro.getProductId());
    			}
    			busPro.setUpdateTime(DateUtils.getNowDate());
    			
    			//计算产品价格
    			totalPrice = totalPrice.add(busPro.getSalesPrice().multiply(busPro.getNum()));
        	}
    		//更新商机表商机金额
    		CrmBusiness business = crmBusinessService.selectCrmBusinessById(crmBusProductList.get(0).getBusinessId());
    		business.setMoney(totalPrice.subtract(totalPrice.multiply(business.getDiscountRate()).divide(new BigDecimal(100), 20, BigDecimal.ROUND_HALF_UP)));
    		business.setTotalPrice(totalPrice.subtract(totalPrice.multiply(business.getDiscountRate()).divide(new BigDecimal(100), 20, BigDecimal.ROUND_HALF_UP)));
    		crmBusinessService.updateCrmBusiness(business);
    		
    		return crmBusinessProductMapper.batchCrmBusinessProduct(crmBusProductList);
    	}else {
    		return 1;
    	}
		
//    	for(CrmBusinessProduct busPro : crmBusProductList) {
//    		if(null != busPro.getId()) {
//    			busPro.setUpdateTime(DateUtils.getNowDate());
//    			busPro.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
//    			crmBusinessProductMapper.updateCrmBusinessProduct(busPro);
//    		}else {
//    			busPro.setCreateTime(DateUtils.getNowDate());
//    			busPro.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
//    			busPro.setUpdateTime(DateUtils.getNowDate());
//    			crmBusinessProductMapper.insertCrmBusinessProduct(busPro);
//    		}
//    	}
    	
    }

    /**
     * 修改合同产品关系
     * 
     * @param crmBusinessProduct 商机产品关系
     * @return 结果
     */
    @Override
    public int updateCrmContractProduct(List<CrmBusinessProduct> crmBusProductList, CrmContract contract)
    {
    	//删除商机所有商品后重新添加
		crmBusinessProductMapper.deleteCrmBusProductByBusId(contract.getId(), Long.valueOf(2));
    	if(crmBusProductList.size() > 0) {
    		
    		BigDecimal totalPrice = new BigDecimal(0);
    		for(CrmBusinessProduct busPro : crmBusProductList) {
    			busPro.setBusinessId(contract.getId());
    			busPro.setCreateTime(DateUtils.getNowDate());
    			busPro.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
    			if(null == busPro.getProId()) {
    				busPro.setProId(busPro.getProductId());
    			}
    			busPro.setUpdateTime(DateUtils.getNowDate());
    			busPro.setSubtotal(busPro.getSalesPrice().multiply(busPro.getNum()));
    			
    			//计算产品价格
    			totalPrice = totalPrice.add(busPro.getSalesPrice().multiply(busPro.getNum()));
        	}
    		//更新商机表商机金额
//    		contract.setMoney(totalPrice.subtract(totalPrice.multiply(contract.getDiscountRate()).divide(new BigDecimal(100), 20, BigDecimal.ROUND_HALF_UP)));
//    		contract.setTotalPrice(totalPrice);
    		crmContractMapper.updateCrmContract(contract);
    		
    		return crmBusinessProductMapper.batchCrmBusinessProduct(crmBusProductList);
    	}else {
    		return 1;
    	}
    }

    /**
     * 批量删除商机产品关系
     * 
     * @param ids 需要删除的商机产品关系ID
     * @return 结果
     */
    @Override
    public int deleteCrmBusinessProductByIds(Long[] ids)
    {
        return crmBusinessProductMapper.deleteCrmBusinessProductByIds(ids);
    }

    /**
     * 删除商机产品关系信息
     * 
     * @param id 商机产品关系ID
     * @return 结果
     */
    @Override
    public int deleteCrmBusinessProductById(Long id)
    {
        return crmBusinessProductMapper.deleteCrmBusinessProductById(id);
    }
}
